package cuit.epoch.pymjl.security.controller;

import cuit.epoch.pymjl.security.common.result.CommonResult;
import cuit.epoch.pymjl.security.common.result.Result;
import cuit.epoch.pymjl.security.common.result.ResultUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.Random;

@RestController
@CrossOrigin //允许跨域访问
@Validated
public class EmailController {

    @Autowired
    JavaMailSender javaMailSender; //可直接注入邮件发送的对象

    @Value("${spring.mail.username}") //动态获取配置文件中 发送邮件的账户
    private String sendemail;

    @GetMapping("/getEmailCode")
    public CommonResult<Integer> getEmailCode(@RequestParam String email){

        //随机生成一个四位数的验证码
        int yzm = new Random().nextInt(999999) % (999999 - 100000 + 1) + 100000;
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom("1771477087@qq.com");
        //谁要接收
        message.setTo(email);
        //邮件标题
        message.setSubject("欢迎注册闲置商品网站！");
        //邮件内容
        message.setText("您的验证码是"+yzm);
        javaMailSender.send(message);
        System.out.println(yzm);
        return ResultUtils.success(yzm);
    }
}
